import * as THREE from 'three';
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js';
import {OrbitControls} from 'three/examples/jsm/controls/OrbitControls.js';
import {TransformControls} from 'three/examples/jsm/controls/TransformControls.js';

// import {DragControls} from 'three/examples/jsm/controls/DragControls.js';
// import {FlyControls} from 'three/examples/jsm/controls/FlyControls.js';
// import {FirstPersonControls} from 'three/examples/jsm/controls/FirstPersonControls.js';

const gltfLoader = new GLTFLoader();
const textureLoader = new THREE.TextureLoader();

const container = document.getElementById('app');
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.z = 5;
const renderer = new THREE.WebGLRenderer();        
renderer.setSize(window.innerWidth, window.innerHeight);          
    
container.appendChild(renderer.domElement); 

//所有导出的对象到app中
export const app= {
  scene,
  camera,
  renderer,
  gltfLoader,
  textureLoader,
}

//动画循环
export function animate() {
    requestAnimationFrame(animate);   
   
    renderer.render(scene, camera);
}

// 异步加载场景物体
export function loadGltfAysnc(params) {  
    const {url,onProgress}=params
    return new Promise((resolve, reject) => {
      gltfLoader.load(url, (gltf) => {      
        resolve(gltf)  
      },(progress)=> {    
        if(onProgress){
          onProgress(progress)
        }
      },(err)=>{
        reject(err)
      });   
    });      

  }
// 异步加载纹理贴图
export function  loadTextureAysnc(params) {
    const {url}=params
    return new Promise((resolve, reject) => {
      textureLoader.load(url, (texture) => {      
        resolve(texture)  
      },undefined,(err)=>{
        reject(err)
      });   
    });      
}


